//
// Created by Administrator on 2021/11/9.
// May Saint Diana bless you!
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    vector<vector<int>> diagonalSort(vector<vector<int>> &mat) {
        unordered_map<int, priority_queue<int, vector<int>, greater<>>> um;
        unsigned int m = mat.size(), n = mat[0].size();
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                um[j - i].push(mat[i][j]);
            }
        }
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                mat[i][j] = um[j - i].top();
                um[j - i].pop();
            }
        }
        return mat;
    }
};

int main() {
    vector<vector<int>> matrix{{11, 25, 66, 1,  69, 7},
                               {23, 55, 17, 45, 15, 52},
                               {75, 31, 36, 44, 58, 8},
                               {22, 27, 33, 25, 68, 4},
                               {84, 28, 14, 11, 5,  50}};
    Solution solution;
    vector<vector<int>> ans = solution.diagonalSort(matrix);
    for (const auto &row: ans) {
        for (const auto &x: row) {
            cout << x << " ";
        }
        cout << endl;
    }
    return 0;
}
